package org.oproject.rmi.hessian.core.service;

import java.lang.reflect.InvocationTargetException;

import org.oproject.rmi.hessian.core.job.HessianJob;
import org.oproject.rmi.hessian.util.MethodUtil;


/**
 * 默认的服务管理对象
 * @see
 * @author shenqing
 * @since 1.0
 * @version 1.0, Nov 15, 2010 6:06:48 PM
 */
public class DefaultServiceManager extends ServiceManager 
{	
	/**
	 * 执行服务
	 * @param serviceName  服务名称
	 * @param args         执行服务需要的参数
	 * @return
	 */
	@Override
	public Object invokeService(HessianJob hessianJob)
	{
		if(null == hessianJob.getServiceName() || hessianJob.getServiceName().trim().equals(""))
		{
			throw new RuntimeException("serviceName conn't be null...");
		}
		
		ServiceInfo serviceInfo = getServiceInfo(hessianJob);
		Object serviceObj = getService(serviceInfo);
		if(null != serviceObj)
		{
			try
			{
				return MethodUtil.invokeMethod(serviceObj, serviceInfo.getMethodName(), (String[])hessianJob.getArgs());
			}
			catch (InvocationTargetException e)
			{
				throw new RuntimeException(e.getCause());
			}
			catch (NoSuchMethodException e)
			{
				throw new RuntimeException(e.getCause());
			} 
			catch (IllegalArgumentException e) 
			{
				throw new RuntimeException(e.getCause());
			}
		}
		else
		{
			throw new RuntimeException("can't not found service...");
		}
	}
}
